import java.util.StringTokenizer;

/**
 * This helper class processes the string data uploaded by gps tracker.  
 * @author Xiaohui
 *
 */
public class StrParser {

	/**
	 * Fields to be extracted from gps data
	 */
	private String trimmedStr;
	private String imei;
	private String uploadTime;
	private String Aattr;
	private String Nattr;
	private String Eattr;

	/**
	 * GPS string data is processed into fields here.  
	 * @param str
	 */
	public StrParser(String str) {
		if (str == null) {
			System.err.println("StrParser: string is null");
			System.exit(0);
		}
		trimmedStr = str.trim();
		if (trimmedStr == "") {
			System.err.println("StrParser: string is empty");
			System.exit(0);
		}
		StringTokenizer st = new StringTokenizer(str, ",");
		uploadTime = st.nextToken();
		if (!Character.isDigit(uploadTime.charAt(0))) {
			uploadTime = null;
		}
		while (st.hasMoreTokens()) {
			String temp = st.nextToken().trim();
			if (temp.startsWith("imei:")) {
				int semicol = temp.indexOf(":");
				imei = temp.substring(semicol + 1);
			} else {
				String t1 = null;
				if (st.hasMoreElements()) {
					t1 = st.nextToken().trim();
					if (t1.startsWith("imei:")) {
						int semicol = t1.indexOf(":");
						imei = t1.substring(semicol + 1);
					}
				} else {
					break;
				}

				if (t1.equals("A")) {
					Aattr = temp;
				} else if (t1.equals("N")) {
					Nattr = temp;
				} else if (t1.equals("E")) {
					Eattr = temp;
				}
			}
		}
	}

	/**
	 * @return the imei
	 */
	public String getImei() {
		return imei;
	}

	/**
	 * @return the uploadTime
	 */
	public String getUploadTime() {
		if (uploadTime == null) {
			return null;
		}
		return parseTime(uploadTime);
	}

	/**
	 * Time is formatted here.
	 * @param timestr
	 * @return
	 */
	private String parseTime(String timestr) {
		String year = "20" + timestr.substring(0, 2);
		String month = timestr.substring(2, 4);
		String day = timestr.substring(4, 6);
		String hr = timestr.substring(6, 8);
		String min = timestr.substring(8, 10);
		String sec = timestr.substring(10, 12);
		return year + "-" + month + "-" + day + " " + hr + ":" + min + "" + ":"
				+ sec;
	}

	/**
	 * @return the aattr
	 */
	public String getAattr() {
		return Aattr;
	}

	/**
	 * @return the nattr
	 */
	public String getNattr() {
		return parseDeg(Nattr);
	}

	/**
	 * The latitude and longitude are processed here. 
	 * @param nattr
	 * @return
	 */
	private String parseDeg(String nattr) {
		// System.out.println("nattr:"+nattr);
		String temp = Double.parseDouble(nattr) / 100 + "";
		int dotInx = temp.indexOf(".");
		String t1 = temp.substring(0, dotInx);
		String t2 = temp.substring(dotInx + 1);

		return t1 + "." + (Long.parseLong(t2) / 60);

	}

	/**
	 * @return the eattr
	 */
	public String getEattr() {
		return parseDeg(Eattr);
	}

	/**
	 * @return Example parser
	 */
	public static StrParser getExampleParser() {
		String sample = "091111090338,96617048,GPRMC,090338.369,A,0117.9108,N,10346.3980,E,0.00,0.00,111109,,,A*6F,F,, imei:354776033626547,07,168.8,F:3.97V,0,134,39409";
		StrParser parser = new StrParser(sample);
		return parser;
	}

	public static void main(String[] args) {
		String sample = "091111090338,96617048,GPRMC,090338.369,A,0117.9108,N,10346.3980,E,0.00,0.00,111109,,,A*6F,F,, imei:354776033626547,07,168.8,F:3.97V,0,134,39409";
		StrParser parser = new StrParser(sample);
		System.out.println(parser.getImei());
		System.out.println(parser.getUploadTime());
		System.out.println(parser.getNattr());
		System.out.println(parser.getEattr());
	}
}
